package com.epfresh.adapter;

import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.api.universalimageloader.core.DisplayImageOptions;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.universalimageloader.core.assist.ImageScaleType;
import com.epfresh.bean.ListStore;

import multitype.ItemViewBinder;

/**
 * Author: Chen
 * Date: 2019/2/21
 * Desc:
 */
public class SearchStoreViewBinder extends ItemViewBinder<ListStore, SearchStoreViewBinder.ViewHolder> {
    private OnStoreClickListener onStoreClickListener;

    public void setOnStoreClickListener(OnStoreClickListener onStoreClickListener) {
        this.onStoreClickListener = onStoreClickListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View itemView = inflater.inflate(R.layout.item_merchant, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull final ListStore item) {
        holder.rl_title.setVisibility(View.GONE);
        holder.txt_wholesale_market.setVisibility(View.VISIBLE);
        ImageLoader.getInstance().displayImage(item.getHeaderImage(),holder.img_logo,options);
        holder.txt_merchant_name.setText(item.getName());
        holder.txt_order_count.setText("订单量" + item.getOrderCount());
        holder.txt_goods_count.setText("商品" + item.getOnShelvesProductCount());
        holder.txt_rating.setText("好评" + item.getGoodCommentsRate());
        holder.txt_wholesale_market.setText(item.getStall());

        if (item.getRecType() == 0) {
            holder.img_status.setVisibility(View.GONE);
        } else {
            holder.img_status.setVisibility(View.VISIBLE);
            if (item.getRecType() == 1) {
                holder.img_status.setImageResource(R.mipmap.icon_bought);
            }
            if (item.getRecType() == 2) {
                holder.img_status.setImageResource(R.mipmap.icon_attention);
            }
            if (item.getRecType() == -1) {
                holder.img_status.setImageResource(R.mipmap.icon_resting);
            }
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onStoreClickListener != null) {
                    onStoreClickListener.onStoreClick(item);
                }
            }
        });


    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private View rl_title;
        private ImageView img_logo,img_status;
        private TextView txt_merchant_name,txt_order_count,txt_goods_count,txt_rating;
        private TextView txt_wholesale_market;

        ViewHolder(View itemView) {
            super(itemView);
            rl_title = itemView.findViewById(R.id.rl_title);
            img_logo = itemView.findViewById(R.id.img_logo);
            img_status = itemView.findViewById(R.id.img_status);
            txt_merchant_name = itemView.findViewById(R.id.txt_merchant_name);
            txt_order_count = itemView.findViewById(R.id.txt_order_count);
            txt_goods_count = itemView.findViewById(R.id.txt_goods_count);
            txt_rating = itemView.findViewById(R.id.txt_rating);
            txt_wholesale_market = itemView.findViewById(R.id.txt_wholesale_market);
        }
    }

    public interface OnStoreClickListener {
        void onStoreClick(ListStore item);
    }

    private DisplayImageOptions options = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.mipmap.icon_fail_merchant) //设置图片在下载期间显示的图片
            .showImageForEmptyUri(R.mipmap.icon_fail_merchant)//设置图片Uri为空或是错误的时候显示的图片
            .showImageOnFail(R.mipmap.icon_fail_merchant)
            .cacheInMemory(true)// 设置下载的图片是否缓存在内存中
            .cacheOnDisk(true)// 设置下载的图片是否缓存在SD卡中
            .considerExifParams(true) // 是否考虑JPEG图像EXIF参数（旋转，翻转）
            .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)// 设置图片以如何的编码方式显示
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();



}
